/**
 * ExtGames.MainWindow
 * @extends Ext.Window
 * Renders a Solitaire game
 */
ExtGames.MainWindow = function(config) {  
  this.game = config.game;
  
  var opp = util.getOpponents();
    
  var comps = [];
  
  for (var i = 0; i < opp.length; i++) {
	  comps.push(config.hands[i+1]);
	  if (config.playareas[i+1]) {
		  for (var j = 0; j < config.playareas[i+1].length; j++) {
			  comps.push(config.playareas[i+1][j]);
		  }
	  }
  }
      
  this.otherPlayerPanel = new Ext.Panel({
	    layout: 'column',
	    region: 'north',
	    height: 125,
	    cmargins: '5 0 0 0'	,
		html: '',
		items: comps
  });
  
  comps = [];
  
  this.unuseddeck = config.unuseddeck;
  this.discardpile = config.discardpile;
  comps.push(config.unuseddeck);
  comps.push(config.discardpile);
    
  comps.push(config.hands[0]);
  if (config.playareas[0]) {
	  for (var i = 0; i < config.playareas[0].length; i++) {
		  comps.push(config.playareas[0][i]);
	  }
  }
    
  this.bottomPanel = new Ext.Panel({
	  id: 'my-panel',
	  layout: 'column',
	    region: 'center',
	    height: 125,
	    cls: 'bottom-table',
	    cmargins: '5 0 0 0',
	    defaults: {
	        border: false
	      },
  		items: comps
  });
    
  this.rightPanel = config.scorecard;
  
  this.mainPanel = new Ext.Panel({
	  layout: 'border',
	  region: 'center',
	  defaults: {
	        border: false
	      },
	  items: [
		  //this.decksPanel,
		  this.bottomPanel,
		  this.otherPlayerPanel
		  ]
  });
      
  this.bbar = new Ext.StatusBar({
	id: 'status_bar',
    statusAlign: 'left',
    text: ''
  });
 
 
  var tbarItems = [];
  tbarItems.push({
	text:    'New Game',
    scope:   this,
    handler: function() {
    	this.game.newGameClicked();
    }
  });
 
 
	tbarItems.push({
      	id: 'nextRoundButton',
      	text: 'Next Round',
      	scope: this,
      	disabled: true,
      	handler: function() {
      		util.submitNextRound();
        	Ext.getCmp('nextRoundButton').disable();
      	}
    });
    
    tbarItems.push({
      	id: 'autoSortButton',
      	text: 'Auto-Sort',
      	enableToggle: true,
      	allowDepress: true,
      	pressed: true,
      	scope: this,
      	toggleHandler: function(button, state) {
      		config.hands[0].toggleAutoSort(state);
      	}
    });
    
    tbarItems.push('-');
    
    var extraButtons = config.game.getToolbarButtons();
    for (var i = 0; i < extraButtons.length; i++) {
    	tbarItems.push(extraButtons[i]);
    }
		
 
  this.tbar = new Ext.Toolbar({
  	id: 'tool_bar',
  	items: tbarItems
  });

  this.panel = new Ext.Panel({
    title:     'ExtGames',
    layout:'border',
    
    tbar: this.tbar,
    
    bbar: this.bbar,
    
    defaults: {
      border: false
    },
    
    items: [
      this.mainPanel,
      this.rightPanel
    ]
  });
  
  this.removeComponents =  function() {
  	while (this.otherPlayerPanel.items.length > 0) {
 		this.otherPlayerPanel.remove(this.otherPlayerPanel.items.last());
  	  }
  	  
  	  while (this.bottomPanel.items.length > 0) {
 		this.bottomPanel.remove(this.bottomPanel.items.last());
  	  }
  };
  
  this.redraw = function(hands, playareas) {
  	  
  	  
  	  
  	  this.bottomPanel.add(this.unuseddeck);
  	  this.bottomPanel.add(this.discardpile);
  	  this.bottomPanel.add(hands[0]);
  	  for (var i = 0; i < playareas[0].length; i++) {
  	  	this.bottomPanel.add(playareas[0][i]);
  	  }
  	  
	  for (var i = 1; i < hands.length; i++) {
		  this.otherPlayerPanel.add(hands[i]);
		  for (var j = 0; j < playareas[i].length; j++) {
			  this.otherPlayerPanel.add(playareas[i][j]);
		  }
	  }
	  
	  this.panel.doLayout();
  	  
  };
  
  this.updateStatus = function(status) {
	  Ext.getCmp('status_bar').setText(status);
  };
  
  this.render = function() {
  	this.viewport = new Ext.Viewport({
  		
		cls: 'background-green',
  		layout: 'fit',
  		items: [this.panel]

  	});
  }
  
}